package social;

import java.io.IOException;
import java.net.URLDecoder;
import java.text.ParseException;
import java.util.StringTokenizer;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import service.Socialgraph;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;

import data.Data;

@SuppressWarnings("serial")
public class HomeServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		
		String name = URLDecoder.decode(req.getParameter("name"),"UTF-8");
		String update = req.getParameter("update");
		
		DBCollection coll = Data.data("link");
		DBObject cursor = coll.findOne(new BasicDBObject("name",name));

		if(cursor != null)
		{
			req.setAttribute("cursor", cursor);
			try {
				req.getRequestDispatcher("/home.jsp").forward(req, resp);
			} catch (Exception e) {
				// TODO: handle exception
			}
		} else {
			if(update == null)
			{
				try {
					new Socialgraph().Soc(name);
					resp.sendRedirect("/insert?name="+name+"&update=true");
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else {
				resp.getWriter().println("Url not query");
			}
		}
	}
}
